﻿fifachamps.core.access.push("players");

fifachamps.players = {
    initialize: function() {
        //create the panel
        var player_panel = new Ext.Panel({
            id: 'PNL_players',
            title: 'Players',
            layout: 'column',
            autoHeight: true,
            autoWidth: true,
            closable: false
        });

        //create the form
        var form_panel = new Ext.Panel({
            id: 'PNL_player_form',
            title: 'Capture Player Information',
            layout: 'form',
            columnWidth: 0.5,
            height: '100%',
            autoWidth: true,
            border: false, 
            bodyPadding: 15,
            items: [
                {
                    xtype: 'textfield',
                    id: 'TXT_player_name',
                    fieldLabel: 'Name',
                    allowBlank: false
                }, {
                    xtype: 'textfield',
                    id: 'TXT_player_surname',
                    allowBlank: false,
                    fieldLabel: 'Surname'
                }, {
                    xtype: 'textfield',
                    id: 'TXT_player_email',
                    vtype: 'email',
                    fieldLabel: 'Email'
                }, {
                    xtype: 'textfield',
                    id: 'TXT_player_phone',
                    fieldLabel: 'Phone'
                }, {
                    xtype: 'textfield',
                    id: 'TXT_player_nick',
                    allowBlank: false,
                    fieldLabel: 'Nickname/Online ID'
                }, { 
                    xtype: 'radiogroup',
                    fieldLabel: 'Online',
                    id: 'RAD_online',
                    vertical: true,
                    columns: 4,
                    allowBlank: false,
                    items: [
                        { boxLabel: 'No', name: 'rb', inputValue: 'no'},
                        { boxLabel: 'PSN', name: 'rb', inputValue: 'psn'},
                        { boxLabel: 'XBox', name: 'rb', inputValue: 'xbox'},
                        { boxLabel: 'PC', name: 'rb', inputValue: 'pc'}
                    ]
                }
            ], 
            buttonAlign: 'center',
            buttons: [
                { id: 'BTN_submit', formBind: true, text: 'Save', handler: function(btn, evt) { fifachamps.players.save_player(btn, evt); } },
                { id: 'BTN_clear', text: 'Clear', handler: function() { fifachamps.players.clear(); }  }
            ]
        });

        player_panel.add(form_panel);

        ///create list view
        var list_height = Ext.get('TABPNL_main-body').dom.scrollHeight - 25;
        var store = this.data.player_store();    
        store.load({params: { start: 0, limit: 25, page: 1, cmd: 'get_players' } });    
        var list = new Ext.ListView({
            id: 'LST_players',
            store: store,
            height: list_height,
            columns: [
                {header: 'Name', dataIndex: 'Name', width: 150},
                {header: 'Surname', dataIndex: 'Surname', width: 150},
                {header: 'Nick', dataIndex: 'Nickname', width: 150}
            ]
        });

        ///create list view panel
        var list_panel = new Ext.Panel({
            id: 'PNL_listview_players',
            title: 'View players',
            columnWidth: 0.5,
            border: false//,
//            dockedItems: [{
//                xtype: 'pagingtoolbar',
//                id: 'PTB_players',
//                store: store,
//                dock: 'bottom',
//                displayInfo: true
//            }]
        });        
        
        //add the child panels to the parent
        list_panel.add(list);
        player_panel.add(list_panel);
        fifachamps.core.tabpanel.add_tab(player_panel);
    },
    save_player: function(btn, evt) {
        var txt_player_name = Ext.getCmp('TXT_player_name');
        var txt_player_surname = Ext.getCmp('TXT_player_surname');
        var txt_player_email = Ext.getCmp('TXT_player_email');
        var txt_player_phone = Ext.getCmp('TXT_player_phone');
        var txt_player_nick = Ext.getCmp('TXT_player_nick');
        var txt_player_online = Ext.getCmp('RAD_online');
        var success = true;

        if (txt_player_name.validate() &&
            txt_player_surname.validate() &&
            txt_player_email.validate() &&
            txt_player_phone.validate() && 
            txt_player_nick.validate() &&
            txt_player_online.validate() )
        {
            var pname = txt_player_name.getValue();
            var psurname = txt_player_surname.getValue();
            var pemail = txt_player_email.getValue();
            var pphone = txt_player_phone.getValue();
            var pnick = txt_player_nick.getValue();
            var po = txt_player_online.getValue().rb;

            Ext.Msg.show({
                 title:'Save Changes?',
                 msg: 'You are about to save ' + pname + '. Are you sure you want to continue?',
                 buttons: Ext.Msg.YESNO,
                 icon: Ext.Msg.QUESTION,
                 fn: function(btn) { 
                    if (btn == "yes") {
                        Ext.getCmp("PNL_players").getEl().mask("Saving...");
                        fifachamps.core.post("save_player",
                            {
                                id: 0,
                                name: pname,
                                surname: psurname,
                                email: pemail,
                                phone: pphone,
                                nick: pnick,
                                online: po 
                            },
                            function(data) { 
                                Ext.getCmp("PNL_players").getEl().unmask();
                                Ext.StoreManager.lookup('STR_players').load();
                            }
                        );
                    }
                 }
            });
        }
    },
    clear: function() {
        Ext.getCmp('TXT_player_name').setValue("");
        Ext.getCmp('TXT_player_surname').setValue("");
        Ext.getCmp('TXT_player_email').setValue("");
        Ext.getCmp('TXT_player_phone').setValue("");
        Ext.getCmp('TXT_player_nick').setValue("");
        Ext.getCmp('RAD_online').setValue("");
    },
    data: {
        store: {
        },
        player_store: function() {
            var store = fifachamps.core.base_store({
                StoreID: 'STR_players',
                Fields: [ "ID", "Name", "Surname", "Email", "Phone", "Nickname", "Online", "FirstTeam", "SecondTeam", "JoinDate", "Registered"],
                Root: 'data',
                Proxy: 'proxy.ashx',
                PageSize: 25
            });

            store.on('beforeload', function() {
                store.getProxy().extraParams.cmd = 'get_players'  
            });

            return store;
        }
    }
};